import Vue from 'vue';
import store from '../store';

const plugin = {
  install(Vue) {
    Vue.prototype.$dict = (type) => {
      const dict = store.state.dict.dict;
      return {
        get(key) {
          const dictType = dict?.[type];
          let result = null;
          dictType.forEach((item) => {
            if (item.value === key) {
              result = item.text;
            }
          });
          return result ?? key ?? '--';
        },
        all() {
          return dict?.[type] ?? [];
        },
        list(labelKey = 'label', valueKey = 'value') {
          const dictType = dict?.[type];
          let value = [];
          if (dictType.length > 0) {
            value = dictType.map((item) => {
              return {
                [labelKey]: item.text,
                [valueKey]: item.value,
              };
            });
          }
          return value;
        },
      };
    };
  },
};

Vue.use(plugin);

//加载字典
